package id.xsis.project.blog.controller;

import id.xsis.project.blog.dao.DatabaseDao;
import id.xsis.project.blog.entity.User;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value = "master/*")
public class UserController {

	@Autowired
	DatabaseDao databaseDao;

	// list artikel
	public String listUser(ModelMap modelMap) {
		List<User> user = databaseDao.listUser();
		modelMap.put("users", user.toArray());
		return "/master/user/searchUser";
	}

	// UPDATE
	@RequestMapping(value = "user/detailUser", method = RequestMethod.POST)
	public String updateUser(@ModelAttribute User user) {

		databaseDao.saveUser(user);
		return "redirect:searchUser";
	}

	// INSERT
	@RequestMapping(value = "user/insertUser")
	public String insertUser() {
		return "/master/user/insertUser";
	}

	@RequestMapping(value = "user/insertUser", method = RequestMethod.POST)
	public String saveBlog(@ModelAttribute User user) {

		databaseDao.saveUser(user);
		return "redirect:searchUser";
	}

	// Delete
	@RequestMapping(value = "user/deleteUser")
	public String deleteUser(User user) {
		databaseDao.deleteUser(user);
		return "redirect:searchUser";
	}

}
